package java8Up.stream;

import com.sun.istack.internal.NotNull;

import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Fang Ruichuan
 * @date 2022-11-07 13:12
 */
public class top3LongWords {
    public static void main(String[] args) {
//        sortGetTop3LongWords("What,can,affect,my,mood,is,not,the,scenery,not,the,weather,not,the,time,but,the,person,I,care,about")
//                .forEach(System.out::println);
//        System.out.println("------------------");
//        stringToIntFlatmap().forEach(System.out::println);
//        System.out.println("------------------");
//        testPeekAndForeach();
//        testGetTargetUsers();
//        System.out.println("-----------");
//        testHandleStreamAfterClosed();
//        System.out.println("--------");
//        testCollectJoinStrings();
        testNumberCalculate();
    }

    // 数据批量运算
    public static void testNumberCalculate() {
        List<Integer> ids = Arrays.asList(10, 20, 30, 40, 50);
        // 计算平均值
        Double avg = ids.stream().collect(Collectors.averagingInt(value -> value));
        System.out.println("平均值：" + avg);
        // 数据统计信息
        IntSummaryStatistics summary = ids.stream().collect(Collectors.summarizingInt(value -> value));
        System.out.println("数据统计信息：" + summary);
    }

    public static void testCollectJoinStrings() {
        List<String> ids = Arrays.asList("205","10","308","49","627","193","111", "193");
        String result = ids.stream().collect(Collectors.joining(","));
        System.out.println(result);
    }

    // 从给定句子中返回单词长度大于5的单词列表，按长度倒序输出，最多返回3个
    public static List<String> sortGetTop3LongWords(@NotNull String sentence) {
        return Arrays.stream(sentence.split(","))
                .filter(word -> word.length() > 5)
                .sorted((o1, o2) -> o2.length() - o1.length())
                .limit(3)
                .collect(Collectors.toList());
    }

    // 现有一个句子列表，需要将句子中每个单词都提取出来得到一个所有单词列表
    public static List<String> stringToIntFlatmap() {
        List<String> sentences = Arrays.asList("hello world", "Jia Gou Wu Dao");
        return sentences.stream()
                .flatMap(sentence -> Arrays.stream(sentence.split(" ")))
                .collect(Collectors.toList());
    }

    public static void testPeekAndForeach() {
        List<String> sentences = Arrays.asList("hello world", "Jia Gou Wu Dao");
        // 仅peek操作，最终不会执行
        System.out.println("----before peek----");
        sentences.stream().peek(sentence -> System.out.println(sentence));
        System.out.println("----after peek----");
        // 仅foreach操作，最终会执行
        System.out.println("----before foreach----");
        sentences.stream().forEach(sentence -> System.out.println(sentence));
        System.out.println("----after foreach----");
        // peek操作后面增加终止操作，peek会执行
        System.out.println("----before peek and count----");
        sentences.stream().peek(sentence -> System.out.println(sentence)).count();
        System.out.println("----after peek and count----");
    }

    public static void testGetTargetUsers() {
        List<String> ids = Arrays.asList("205","10","308","49","627","193","111", "193");
        List<User> results = ids.stream()
                .filter(s -> s.length() > 2)
                .distinct()
                .map(Integer::valueOf)
                .sorted(Comparator.comparingInt(o -> o))
                .limit(3)
                .map(id -> new User(id))
                .collect(Collectors.toList());
        System.out.println(results);
    }

    public static void testHandleStreamAfterClosed() {
        List<String> ids = Arrays.asList("205","10","308","49","627","193","111", "193");
        Stream<String> stream = ids.stream().filter(s -> s.length() > 2);
        System.out.println(stream.count());
        System.out.println("-----下面会报错-----");
        // 判断是否有元素等于205
        try {
            System.out.println(stream.anyMatch("205"::equals));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("-----上面会报错-----");
    }
}

class User {
    private Integer id;

    public User(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                '}';
    }
}
